package com.socket;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class EchoServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(9991);
            Socket client = serverSocket.accept();
            Scanner scanner = new Scanner(client.getInputStream());

            PrintWriter out = new PrintWriter(client.getOutputStream());
            boolean flag = true;
            System.out.println("echo 已启动。。。。。");
            while (flag) {
                if (scanner.hasNext()) {
                    String value = scanner.next().trim();
                    if (value.equalsIgnoreCase("exit")) {
                        out.println("Server 信息交互完毕,已经断开与服务器的连接");
                        flag =false;
                        break;
                    }
                    out.println("ECHO server:" + value);
                }
            }
            serverSocket.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
